<?php 
namespace Home\Controller;
use Think\Controller;
class PersonController extends Controller
{
	public function _initialize()
	{
        // var_dump($_SESSION);exit;
		if(empty($_SESSION['home_user'])){
			$this->error('你还未登入,正在前往登入页面',U('Index/index'));
            exit;
		}
	}
	public function index()
	{
		// exit;
		$id = $_SESSION['home_user']['id'];
		// var_dump($id);exit;
		$data = M('user')->field('phone,name,email,icon')->where('id='.$id)->select();
		$this->assign('user',$data[0]);
		$this->display();
	}
	public function edit()
	{
		$id = $_SESSION['home_user']['id'];
		// echo $id;exit;
		// $id = 2;
		$data = M('user')->field('id,phone,name,email,sex')->where('id='.$id)->select();
		// var_dump($data);exit;
		$this->assign('list',$data[0]);
		$this->display('Person/edit');
	}
	// 上传图片
    public function upload () 
	{
    	$id = I('get.id/d');
    	// echo $id;
    	// exit;
		// $post = $_POST;
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->subName = date('Ymd');
		$upload->savePath = date(''); //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		if (!$info) {
			$this->error($upload->getError());
		} else {
			// var_dump($post);
			// var_dump($info);exit;
			$icon = date('Ymd').$info['icon']['savename'];
			// var_dump($icon);exit;
			$this->success('头像上传成功',U('update',array('id'=>$id,'userpass'=>$_POST['userpass'],'repassword'=>$_POST['repassword'],'phone'=>$_POST['phone'],'name'=>$_POST['name'],'sex'=>$_POST['sex'],'email'=>$_POST['email'],'icon'=>$icon)));
		}
	}
    //执行修改
    public function update()
    {
    	
    	$id = I('get.id/d');
    	// var_dump($_GET);
    	// exit;
    	$user = D('user');
    	// var_dump($user->create($_GET,2));exit;
    	if (!$user->create($_GET,2)) {
    		$this->error($user->getError(),U('edit?id='.$id));
    	} else {
    		// echo $user->where('id='.$id)->fetchSql()->save();exit;
    		//自动验证成功
    		if($user->where('id='.$id)->save()){
    			$this->success('保存成功',U('index'));
	    	}else{
	    		$this->error('编辑失败',U('edit?id='.$id));
	    	}
    	}
    }
    //显示用户订单页
    public function myorder($bz="all")
    {
        // var_dump(time());exit;
    	// var_dump($_SESSION['home_user']['id']);exit;
    	$id = $_SESSION['home_user']['id'];
    	$bz = $_GET['bz'];
    	// var_dump($bz);exit;
    	switch ($bz) {
    		case 'all':
                $count = $data = M('orders')->field('orderNumber')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id)->count();
                $Page = new \Think\Page($count,5);
    			$data = M('orders')->field('lamp_orders.id,orderNumber,amount,lamp_orders.status,s.starttime,s.endtime,c.name,m.name as mname,m.id as mid')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id)->limit($Page->firstRow.','.$Page->listRows)->select();
    			break;
    		case 'arrearage':
                $count = M('orders')->field('orderNumber')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id.' and lamp_orders.status=0')->count();
                $Page = new \Think\Page($count,5);
    			$data = M('orders')->field('lamp_orders.id,orderNumber,amount,lamp_orders.status,s.starttime,s.endtime,c.name,m.name as mname,m.id as mid')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id.' and lamp_orders.status=0')->limit($Page->firstRow.','.$Page->listRows)->select();
    			break;
    		case 'noticket':
                $data = M('orders')->field('orderNumber')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id.' and lamp_orders.status=1')->count();
                $Page = new \Think\Page($count,5);
    			$data = M('orders')->field('lamp_orders.id,orderNumber,amount,lamp_orders.status,s.starttime,s.endtime,c.name,m.name as mname,m.id as mid')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id.' and lamp_orders.status=1')->limit($Page->firstRow.','.$Page->listRows)->select();
    			break;
    		default:
    			$this->error('非法查询');
    			break;
    	}
    	
    	/*var_dump($data);
    	exit;*/
        $Page->setConfig('first','首页'); 
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $show = $Page->show();//
        $this->assign('time',time());
        $this->assign('page',$show); 
    	$this->assign('list',$data);
    	$this->display();
    }
    public function comment()
    {
    	$id = $_SESSION['home_user']['id'];
    	$data = M('orders')->field('orderNumber,amount,s.starttime,s.endtime,c.name,m.name as mname,m.id')->join('lamp_session s on sid=s.id')->join('lamp_cinema c on s.cid=c.id')->join('lamp_movie m on s.mid=m.id')->where('lamp_orders.uid='.$id.' and lamp_orders.status=2')->select();
    	// var_dump($data);exit;
    	$this->assign('list',$data);
    	$this->display();
    }
    public function collection()
    {
        $id = $_SESSION['home_user']['id'];
        $count = M("collection")->where('uid='.$id)->count();
        // var_dump($count);
        $Page = new \Think\Page($count,5);
        $show = $Page->show();
        $data = M('collection')->field('lamp_collection.id,m.name,m.id as mid')->join('lamp_movie m on mid=m.id')->where('uid='.$id)->limit($Page->firstRow.','.$Page->listRows)->select();
        // var_dump($data);exit;
        $this->assign('page',$show);
        $this->assign('list',$data);
        $this->display();
    }
    public function collectionDel($id)
    {
        $id = I('get.id/d');
        if(M('collection')->delete($id) > 0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function orderDel()
    {
        $ordn = I('get.ordernumber');
        if(M('orders')->where('orderNumber='.$ordn)->delete() > 0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function pay()
    {
        // exit;
        $id = I('get.id/d');
        // var_dump($ordn);exit;
        $orders = M('orders');
        $data = $orders->field('orderNumber,sid,uid,amount,time,status')->where('id='.$id)->select()[0];
        $data['status'] = 1;
        // var_dump($data);exit;
        // $sql = "update lamp_orders set status=1 where id=".$id;
        //初始化
        if(!$orders->create($data)){
            $this->error($this->_model->getError());
            exit;
        }
        //执行修改 
        if($orders->where('id='.$id)->save() > 0){
            $this->success("支付成功!");
        }else{
            $this->error("支付失败");
        }
    }
    public function tuikuan()
    {
        $id = I('get.id/d');
        if(empty($id)){
            $this->redirect('Home/Cinema/index');
            exit;
        }
        if(M('orders')->delete($id) > 0){
            $this->success('退款成功',U('Person/myorder',array('bz'=>'all')));
        }else{
            $this->error('退款失败',U('Person/myorder',array('bz'=>'all')));
        }
    }
}





























